<?php
	$_nav='info';
	$_title='小编说';
	include_once 'head.php';

	$infoc=new info();
	$userc=new user();
	$id=$_GET['id'];  //ID

	/******评论start*******/
	if($_GET['action']=='comment'){
		$table='admin_topic_comment';
		$id=$_POST['id'];			    //小编说ID
		$reply_id=$_POST['reply_id'];   //回复ID
		$comment=$_POST['comment'];	    //评论内容
		$user_type=$_POST['user_type'];	//回复类型

		if($user_id!=''){
			$array=array('topic_id'=>$id,'user_id'=>$user_id,'comment'=>$comment,'reply_id'=>$reply_id,'reply_type'=>$user_type,'create_time'=>time());
			//判断是否自己回复自己(暂时关闭)
			// if($reply_id==$user_id){
			// 	$array['reply_id']=0;
			// }
			$res=$commonc->insert($table,$array);
			if($res){
				$mes='评论成功';

				$user=$userc->userInfo($user_id);
				$userName=$user['account'];

				/*回复AJAX-start*/
				if($array['reply_id']>0){
					if($user_type=='user'){
						$reply=$userc->userInfo($reply_id);
						$replyName=$reply['account'];
						$reply="回复&nbsp;@<a href=user_main.php?type=user&id=".$reply_id.">".$replyName."</a>：";
					}else{
						$replyName='小编';
						$reply="回复&nbsp;@<a href=user_main.php?type=admin&id=".$reply_id.">".$replyName."</a>：";
					}	
				}	

                $html='<div class="tctzy_2">
					<p>
				    <a href="user_main.php?type=user&id='.$user_id.'">'.$userName.'</a>：'.$reply.'
					<span class="neirong">'.$comment.'</span></p>
					</div>
					<span class="span_tctzy_2"><a href="javascript:;" class="dan_hf" name='.$userName.' reply_id='.$user_id.' user_type="user">回复</a></span>';		    
			    /*回复AJAX-end*/
			}else{
				$mes='评论失败，请重试';
			}
			$error=1;
		}else{
			$error=0;
		}
		$arr=array('error'=>$error,'mes'=>$mes,'html'=>$html);
		echo json_encode($arr);
		exit();
	}
	/******评论end*******/



	/*******小编说**********/
	//吐槽
	$where1=' AND a.info_id=0';
	$key="a.*,u.name,u.avatar";
	$num='2';
	$comment1=$infoc->topicList($where1,$key,$num);
	//数组重新排序
	if($id!='' && $id!=null){
		if(is_array($comment1['result'])){
			foreach ($comment1['result'] as $k1 => $v1) {
				$ache=$comment1['result']['0'];
				if($v1['id']==$id){
					$comment1['result']['0']=$v1;
					$comment1['result'][$k1]=$ache;
				}
			}
		}
	}

	//分页
	$smarty->assign('page',$comment1['page']);

	//计算总数
	$commentNum=count($comment1['result']);
	$smarty->assign('commentNum',$commentNum);
	//吐槽评论
	$where2='';
	$key="a.*,u.account,u.avatar,u1.account as account1";
	$comment2=$infoc->commentList($where2,$key);
	
	$comment=array();
	if(is_array($comment1['result'])){
		foreach ($comment1['result'] as $k1 => $v1) {
			$comment[$k1]=$v1;					//评论
			if(is_array($comment2['result'])){
				foreach ($comment2['result']as $k2=> $v2) {
					//处理评论跟回复
					if($v2['type']=='user'){
						$v2['userName']=$v2['account'];
					}else{
						$v2['userName']='小编';
					}
					//判断是否属于回复
					if($v2['reply_id']>0){
						if($v2['reply_type']=='user'){
							$v2['replyName']=$v2['account1'];
						}else{
							$v2['replyName']='小编';
						}			
					}
					//组装
					if($v1['id']==$v2['topic_id'] ){
						$comment[$k1]['reply'][]=$v2;
					}	
				}
			}
		}
	}
	$smarty->assign('comment',$comment);


	/******右侧吐槽排行*******/
	$where=' AND a.info_id=0';
	$key="a.*,u.name,u.avatar,(select count(*) from admin_topic_comment where topic_id=a.id) as topicNum";
	$num='7';
	$order=" topicNum desc";
	$ph=$infoc->topicList($where,$key,$num,$order);
	$smarty->assign('ph',$ph['result']);
	/******右侧吐槽排行*******/
	
	$smarty->display("topic_list.html");
?>